package org.jwcloud.project.module.zx.upload.controller;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.db.domain.RtaCMsgUploadFile;
import org.jwcloud.project.module.zx.db.domain.RtaProjectInfo;
import org.jwcloud.project.module.zx.upload.service.FileUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.jw.mid.mm.comm.common.json.Response;
import com.jw.mid.mm.comm.common.pagination.ListSlice;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 文件上传
 * 
 * @author Administrator
 *
 */

@Api(value = "API - Controlle", description = "附件管理", tags = "file")
@RestController
@RequestMapping("/rest/file")

public class FileUploadController {
	/**
	 * 
	 */
	private static final Logger logger = LoggerFactory.getLogger(FileUploadController.class);
	/**
	 * 
	 */
	@Autowired
	private FileUploadService fileUploadService;

	/*
	 * 通过流的方式上传文件
	 * 
	 * @RequestParam("file") 将name=file控件得到的文件封装成CommonsMultipartFile 对象
	 */

	@PostMapping("upload")
	@ApiOperation(value = "文件上传", notes = "")
	@ApiImplicitParams({})
	@ResponseBody
	public Resp<Response<RtaCMsgUploadFile>> upload(@RequestParam("file") MultipartFile file)
			throws IllegalStateException, Exception {
		RtaCMsgUploadFile rtaCMsgUploadFile = fileUploadService.saveFile(file);
		Response<RtaCMsgUploadFile> response = new Response<RtaCMsgUploadFile>();
		response.setCode(0);
		response.setPayload(rtaCMsgUploadFile);
		return Resp.success(response);
	}

	/**
	 * 下载文件
	 * 
	 * @param fileid
	 * @return
	 * @throws Exception
	 */
	@GetMapping("download")
	@ApiOperation(value = "文件下载", notes = "")
	@ApiImplicitParams({})
	@ResponseBody
	public void download(HttpServletResponse response, @RequestParam("fileid") String fileid) throws Exception {
		InputStream input = null;
		// HttpHeaders headers = new HttpHeaders();
		OutputStream  output = null;
		try {
			input = fileUploadService.getFile(fileid);
			if (input != null) {
				RtaCMsgUploadFile rtaCMsgUploadFile = fileUploadService.searchFile(fileid);
				// headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
				// headers.setContentDispositionFormData("attachment", fileid);

				// 解决乱码
				String fileName = rtaCMsgUploadFile.getFileName();
				fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
				response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
				// response.setHeader("Content-Disposition", "attachment;filename=aaa.doc");
				response.setContentType("application/force-download");
				response.setContentType("multipart/form-data");
				response.setCharacterEncoding("UTF-8");
				output=response.getOutputStream();

				byte[] buffer = new byte[8];
				int n = 0;
				while (-1 != (n = input.read(buffer))) {
					output.write(buffer, 0, n);
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw e;
		} finally {
			if (input != null) {
				input.close();
			}
			if (output != null) {
				output.close();
			}
		}

		// return new ResponseEntity<byte[]>(output.toByteArray(), headers,
		// HttpStatus.CREATED);
	}

}
